/*
  * desc  :英雄选择界面
  * author:<zhouzhiwei>
  * @param:
  * date  : 2019/1/28
*/

var GameData = require('../module/GameData')();
cc.Class({
    extends: cc.Component,

    properties: {
        herobtn: {
            default: [],
            type: cc.Sprite
        },
        hero_name: cc.Label,
        hero_price: cc.Label,
        hero: cc.Sprite,
        coinNum: cc.Label,
        hero_list: cc.ScrollView,
        select_hero: cc.Prefab,
        heroItem: [],
        pageId: null,

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {

        GameData.select_hero = this;
        this.content = this.hero_list.content;
        this.list_pool = new cc.NodePool();
        this.selecthero_List();
    },
    selecthero_List() {
        for (var a = 0; a < GameData.hero.length; a++) {
            var item;
            if (this.list_pool.size() > 0) {
                item = this.list_pool.get();
            } else {
                item = cc.instantiate(this.select_hero);
            }
            if (a == 0) {
                item.getChildByName("checkmark").active = true;
            }
            this.heroItem.push(item);
            item.getComponent('selectHero_item').init(GameData.hero[a]);
            this.content.addChild(item);
        }
    },
    selectBtn(evt) {
        var that = this;
        let _name = evt.currentTarget.name;
        switch (_name) {
            case "cancelbtn":/*返回按钮*/
                this.node.active = false;
                cc.find("Canvas/game_start/rank").active = true;
                GameData.game_start.initPeople();
                break;
            case "useBtn":/*装备英雄按钮*/
                this.changebuyBg(0);
                GameData.gameData.selectId = this.pageId;
                GameData.savelocalData(GameData.gameData);
                break;
            case "buybtn":/*购买英雄按钮*/
                if (GameData.gameData.coinNum >= GameData.hero[this.pageId - 1].price) {
                    this.changebuyBg(1);
                    GameData.gameData.owned.push(this.pageId);
                    GameData.savelocalData(GameData.gameData);
                } else {
                    /*金币不足*/
                }
                break;
        }
    },
    /**
     * 更改英雄状态
     * @param type type:0表示已拥有已装备   1;表示已拥有未装备  2表示未拥有
     */
    changebuyBg(type) {
        for (var a = 0; a <  GameData.select_hero.herobtn.length; a++) {
            GameData.select_hero.herobtn[a].node.active = false;
        }
        GameData.select_hero.herobtn[type].node.active = true;
    },
    start() {

    },

    // update (dt) {},
});
